#####################################################################
# Do Personality Traits Predict Voter Attitudes When Politics Is    #
# Structured Around Conflict? Lessons From Israel                   #
#                                                                   #
# Rivlin-Angert, Yakter, and Sheffer                                #
# Public Opinion Quarterly                                          #
#                                                                   #
# Replication code - Figure 1                                       #
#####################################################################
# To load the data, users should place the relevant csv	file in     #
# in their R working directory (or type the full file paths as      #
# saved on their computer).                                         #
#####################################################################

## Required package_groups
library(readstata13)
library(fastDummies)
library(mediation)

## set-up data
# load data
dat <- read.dta13("Angert-Rivlin, Yakter, Sheffer POQ - main survey data.dta")
# break religion to dummies
dat <- dummy_cols(dat, select_columns = "religion")

## primary analyses (Table 4)
# mediation analysis for consc and rwa
set.seed(2023)
med.fit.rwa <- lm(rwa ~ agree + consc + stabi + extra + openn + gender + age_group + educ + religion_2 + religion_3 + religion_4,
              data=dat)
out.fit.rwa <- lm(z1conflict ~ rwa + agree + consc + stabi + extra + openn + gender + age_group + educ + religion_2 + religion_3 + religion_4,
                  data = dat)
med.out.rwa <- mediate(med.fit.rwa, out.fit.rwa, treat = "consc", mediator = "rwa",
                      robustSE = FALSE ,control.value = 0, treat.value = 1, sims = 1000)
summary(med.out.rwa)
# mediation analysis for consc and belong_jewish
med.fit.jew <- lm(belong_jewish ~ agree + consc + stabi + extra + openn + gender + age_group + educ + religion_2 + religion_3 + religion_4,
              data=dat,)
out.fit.jew <- lm(z1conflict ~ belong_jewish + agree + consc + stabi + extra + openn + gender + age_group + educ + religion_2 + religion_3 + religion_4,
              data = dat)
med.out.jew <- mediate(med.fit.jew, out.fit.jew, treat = "consc", mediator = "belong_jewish",
                   robustSE = FALSE ,control.value = 0, treat.value = 1, sims = 1000)
summary(med.out.jew)
# mediation analysis for consc and reject_palestinians
med.fit.pal <- lm(reject_palestinians ~ agree + consc + stabi + extra + openn + gender + age_group + educ + religion_2 + religion_3 + religion_4,
              data=dat,)
out.fit.pal <- lm(z1conflict ~ reject_palestinians + agree + consc + stabi + extra + openn + gender + age_group + educ + religion_2 + religion_3 + religion_4,
              data = dat)
med.out.pal <- mediate(med.fit.pal, out.fit.pal, treat = "consc", mediator = "reject_palestinians",
                   robustSE = FALSE ,control.value = 0, treat.value = 1, sims = 1000)
summary(med.out.pal)

## sensitivity analyses (supplementaru material, section F)
# sensitivity analysis for first model with RWA
sens.out.rwa <- medsens(med.out.rwa, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out.rwa)
# plot - rho (Figure F1)
pdf(file = "sens_rho.pdf", width = 5, height = 4) 
plot(sens.out.rwa, sens.par = "rho")
dev.off()
# plot - R^2 (Figure F2)
pdf(file = "sens_r2.pdf", width = 5, height = 4) 
par(mar=c(5.1,5.5,2.1,2.1))
plot(sens.out.rwa, sens.par = "R2",  r.type = "total", sign.prod = "negative",
     ylim = c(0,0.7), xlim = c(0,0.75),
     ylab = "Proportion of Total Variance in Y\nExplained by Confounder",
     xlab = "Proportion of Total Variance in M\nExplained by Confounder",
     main = "")
dev.off()